package DomainLayer;
import Annotations.AttributeNameAnnotation;
import Annotations.EntityNameAnnotation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

@EntityNameAnnotation(Name = "Commentary")
public class CommentaryEntity extends AbstractEntity implements ICommentable {

	@AttributeNameAnnotation(Name = "id", isAggregator = false)
	private int id;

	@AttributeNameAnnotation(Name = "UserID", isAggregator = true, sourceTableName = "User")
	private UserEntity user;

	@AttributeNameAnnotation(Name = "parentID", isAggregator = true, isHierarhy = true, sourceTableName = "Commentary")
	private CommentaryEntity parent;

    @AttributeNameAnnotation(Name = "Text", isAggregator = false)
    private String text;

    @AttributeNameAnnotation(Name = "Comments", isAggregator = true, isHierarhy = true, sourceTableName = "Commentary")
    private ArrayList<CommentaryEntity> commentaries;

    public CommentaryEntity() {
    }

    public CommentaryEntity(UserEntity user, CommentaryEntity parent, String text) {
        this.user = user;
        this.parent = parent;
        this.text = text;
    }

	public CommentaryEntity getParent() {
		return this.parent;
	}

	public Integer getId() {
		return id;
	}

	public UserEntity getUser() {
		return user;
	}

    public String getText() {
        return text;
    }

	public void setId(int id) {
		this.id = id;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

    public void setText(String text) {
        this.text = text;
    }

    public CommentaryEntity createCommentary(String comment, UserEntity user) {
        CommentaryEntity commentary = new CommentaryEntity(user, this, comment);
        commentaries.add(commentary);
        return commentary;
    }

    public void removeCommentary(int id) {
        for (Iterator<CommentaryEntity> it = commentaries.iterator(); it.hasNext();) {
            if (it.next().getId().equals(id)) {
                it.remove();
                return;
            }
        }
    }

	@Override
	public Collection<CommentaryEntity> getCommentaries() {
		return commentaries;
	}

}